package com.myprocessing.box.button.group;

import com.myprocessing.box.button.Button;
import com.myprocessing.gui.util.Coord;

public class RadioButton extends GroupButton {

	protected Button radioSelected;
	protected Button radioDeselected;
	protected Button currentButton;
	protected boolean isSelected = false;
	

	public void init() {
		super.init();
		currentButton = radioDeselected;
		radioSelected.init();
		radioDeselected.init();
		radioSelected.setParent(this.parent);
		radioDeselected.setParent(this.parent);
	}
	
	public void onMousePress() {
		if(isValidMouseOver()){
			groupButtonManager.onMousePress(this);
			groupButtonExec();
		}
	}
	
	protected void groupButtonExec(){
		toggle();
	}
	
	public void toggle(){
		isSelected = !isSelected;
		
		if(isSelected){
			executeAction(radioSelected.getMousePress());
			currentButton = radioSelected;
		} else {
			executeAction(radioDeselected.getMousePress());
			currentButton = radioDeselected;
		}
	}
	
	public void deselect() {
		isSelected = false;

		executeAction(radioDeselected.getMousePress());
		currentButton = radioDeselected;
	}
	
	public void drawBox() {
		currentButton.display();
	}

	@Override
	public boolean isValidMouseOver() {
//		System.out.println("abs: " + getAbsolutePosition());
		return currentButton.isValidMouseOver();
	}

	@Override
	public void onMouseEnter() {
		currentButton.onMouseEnter();
	}

	@Override
	public void onMouseExit() {
		currentButton.onMouseExit();
	}

	@Override
	public void onMouseOver() {
		currentButton.onMouseOver();
	}

	@Override
	public void onNotMouseOver() {
		currentButton.onNotMouseOver();
	}

	public Button getRadioSelected() {
		return radioSelected;
	}

	public void setRadioSelected(Button radioSelected) {
		this.radioSelected = radioSelected;
	}

	public Button getRadioDeselected() {
		return radioDeselected;
	}

	public void setRadioDeselected(Button radioDeselected) {
		this.radioDeselected = radioDeselected;
	}

	public void setActive(boolean active) {
		super.setActive(active);
		this.radioSelected.setActive(active);
		this.radioDeselected.setActive(active);
		
		if(!active){
			deselect();
		}
	}

	@Override
	public Coord getPosition() {
		return currentButton.getPosition();
	}
}
